//
//  ForgetViewController.m
//  FloatClouds
//
//  Created by 黄威 on 16/8/29.
//  Copyright © 2016年 黄威. All rights reserved.
//

#import "ForgetViewController.h"
#import "AFNetworking.h"
#import "BaseLabel.h"
#import "BaseTextField.h"
#import "BaseButton.h"
#import "Masonry.h"
@interface ForgetViewController ()
@property (nonatomic, retain) BaseLabel *labelOfEmail;
@property (nonatomic, retain) BaseTextField *email;
@property (nonatomic, retain) BaseLabel *labelOfUserName;
@property (nonatomic, retain) BaseTextField *userName;
@property (nonatomic, retain) BaseButton *buttonOfForget;
@property (nonatomic, assign) NSNumber *statusCode;

@end

@implementation ForgetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createView];
}


- (void)createView{

    
    self.labelOfEmail = [[BaseLabel alloc]init];
    self.labelOfEmail.text = @"注册邮箱:";
    [self.labelOfEmail.layer setMasksToBounds:YES];
    [self.labelOfEmail.layer setCornerRadius:5];
    [self.view addSubview:self.labelOfEmail];
    [self.labelOfEmail mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.top.mas_equalTo(74);
        make.width.mas_equalTo(self.view.mas_width).multipliedBy(0.24);
        make.height.mas_equalTo(self.view.mas_height).multipliedBy(0.08);
        
        
    }];
    
    self.email = [[BaseTextField alloc]init];
    self.email.borderStyle = UITextBorderStyleRoundedRect;
    self.email.clearsOnInsertion = YES;
    self.email.clearButtonMode = UITextFieldViewModeAlways;
    [self.view addSubview:self.email];
    [self.email mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.labelOfEmail.mas_right).mas_offset(10);
        make.top.mas_equalTo(74);
        make.width.mas_equalTo(self.view.mas_width).multipliedBy(0.6);
        make.height.mas_equalTo(self.view.mas_height).multipliedBy(0.08);
    }];
    
    self.labelOfUserName = [[BaseLabel alloc]init];
    self.labelOfUserName.text = @"用户名:";
    [self.labelOfUserName.layer setMasksToBounds:YES];
    [self.labelOfUserName.layer setCornerRadius:5];
    [self.view addSubview:self.labelOfUserName];
    [self.labelOfUserName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.top.mas_equalTo(self.labelOfEmail.mas_bottom).mas_offset(20);
        make.width.mas_equalTo(self.view.mas_width).multipliedBy(0.24);
        make.height.mas_equalTo(self.view.mas_height).multipliedBy(0.08);
    }];
    
    self.userName = [[BaseTextField alloc]init];
    self.userName.borderStyle = UITextBorderStyleRoundedRect;
    self.userName.clearsOnInsertion = YES;
    self.userName.clearButtonMode = UITextFieldViewModeAlways;
    [self.view addSubview:self.userName];
    [self.userName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.email);
        make.centerY.mas_equalTo(self.labelOfUserName);
        make.width.mas_equalTo(self.view.mas_width).multipliedBy(0.6);
        make.height.mas_equalTo(self.view.mas_height).multipliedBy(0.08);
    }];
    
    self.buttonOfForget = [BaseButton buttonWithType:UIButtonTypeCustom];
    [self.buttonOfForget setTitle:@"重置密码" forState: UIControlStateNormal];
    self.buttonOfForget.layer.masksToBounds = YES;
    self.buttonOfForget.layer.cornerRadius = 5;
    self.buttonOfForget.backgroundColor = [UIColor colorWithRed:18.0 / 255 green:86.0 / 255 blue:138.0 / 255 alpha:1];
    [self.view addSubview:self.buttonOfForget];
    [self.buttonOfForget mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(self.view.mas_width).multipliedBy(0.6);
        make.height.mas_equalTo(self.view.mas_height).multipliedBy(0.08);
        make.centerY.mas_equalTo(self.view);
        make.centerX.mas_equalTo(self.view);
    }];

    
    [self.buttonOfForget addTarget:self action:@selector(forget:) forControlEvents:UIControlEventTouchUpInside];
    
    

}

- (void)forget:(UIButton *)button{


    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSString *postURLstring = @"http://app.yanyue.cn/user/resetpass";
    
    NSDictionary *parmeters = @{@"email":self.email.text,@"username":self.userName.text};
    

    [manager.requestSerializer setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    NSMutableSet *set = [NSMutableSet setWithSet:manager.responseSerializer.acceptableContentTypes];
    [set addObject:@"text/html"];
    [manager.responseSerializer setAcceptableContentTypes:set];
    
    // 设置请求POST发送
    [manager POST:postURLstring parameters:parmeters progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        self.statusCode = responseObject[@"status"];
        
      
        
        NSLog(@"%@",responseObject);
        
        if ([self.statusCode integerValue] == 1) {
            
            
            UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"成功,已发送邮件请在,邮箱中进行密码重置操作" message:nil preferredStyle:UIAlertControllerStyleAlert];
            [alert addAction:[UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                
            }]];
            
            [self presentViewController:alert animated:YES completion:nil];
            
        }else {
            
            UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"资料与预留资料不一致" message:nil preferredStyle:UIAlertControllerStyleAlert];
            [alert addAction:[UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                
            }]];
            
            [self presentViewController:alert animated:YES completion:nil];
            
            
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"请求失败: %@", error);
    }];




}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
